<template>
  <div class="app-container">

    <div class="filter-container">
      <el-button v-permission="'sys-user-add'" v-waves class="filter-item right-btns" type="success" @click="create()"><svg-icon icon-class="add"/>生成</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="listQuery.list"
      :stripe="true"
      :border="true"
      fit
      highlight-current-row
      style="width: 100%;"
      @selection-change="handleSelectionChange">
      <el-table-column type="index" align="center"/>
      <el-table-column type="selection" width="55"/>
      <el-table-column :label="'日期'" :formatter="formatDate" align="left" min-width="80px" prop="createDate"/>
      <el-table-column :label="'出入别'" align="left" min-width="80px" prop="type" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 0">入</el-tag>
          <el-tag v-if="scope.row.type === 1">出</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="'金额'" align="left" min-width="80px" prop="jine" />
      <el-table-column :label="'方式'" align="left" min-width="80px" prop="flag" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 0">导入</el-tag>
          <el-tag v-if="scope.row.type === 1">录入</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="'备注'" align="left" min-width="80px" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160px">
        <template slot-scope="scope">
          <el-button v-waves type="primary" plain size="mini" @click="edit(scope.row)">修改</el-button>
          <el-button v-waves type="danger" plain size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="listQuery.total>0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.rows"
      @pagination="getList" />
  </div>
</template>

<script>
import { getPages, del, update, add } from '@/api/systemManage/jiaoyijilu'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import DelBoxMixin from '@/mixins/delBox'
import { mapGetters } from 'vuex'

export default {
  name: 'StoreManage',
  components: { Pagination },
  directives: { waves },
  mixins: [DelBoxMixin],

  props: {
    options: {
      type: Object,
      default: function() { return {} }
    }
  },

  data() {
    return {
      listLoading: true,
      listQuery: {}
    }
  },
  computed: {
    ...mapGetters([
      'lang'
    ])
  },
  created() {
    this.tableReset()
  },
  methods: {
    // 时间格式化
    formatDate(row, column) {
      // 获取单元格数据
      const data = row[column.property]
      if (data === '') {
        return ''
      }
      const dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate()
    },
    tableReset() {
      this.listQuery = {
        page: 1,
        rows: 10,
        total: 0,
        shouqianbaId: this.$route.params.id,
        list: null
      }
      this.getList()
    },
    getList() {
      this.listLoading = true
      const params = Object.assign({}, this.listQuery)
      delete params.total
      delete params.list

      getPages(params).then(response => {
        this.listQuery.list = response.data.list
        this.listQuery.total = response.data.total
        this.listLoading = false
      }).catch(e => {
        this.listLoading = false
        this.$message.error(e.message)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSelectionChange(val) {
      this.selectionedList = val
    },
    create() {
    },
    edit(item) {
      if (item && item.id) {
        this.$refs.editDialog.show({
          id: item ? item.id : 0
        })
      } else {
        this.$refs.editDialog.show()
      }
    },

    handleEditOk(data, isEdit) {
      this.isEditSubmitting = true
      const invoke = isEdit ? update : add
      invoke(data).then(resp => {
        this.getList()
        this.$message.success(resp.message)
        this.isEditSubmitting = false
        this.$refs.editDialog.close()
      }).catch(resp => {
        this.$message.error(resp.message)
        this.isEditSubmitting = false
      })
    },
    del(item) {
      this.$$delBox().then(() => {
        del(item.id).then(res => {
          this.$message.success(res.message)
          this.getList()
        }).catch(e => {
          this.$message.error(e.message)
        })
      }).catch(() => {})
    }
  }
}
</script>
